//
//  YZQPhotosViewController.m
//  SuperNews
//
//  Created by qiang on 16/1/13.
//  Copyright © 2016年 qiang. All rights reserved.
//

#import "YZQPhotosViewController.h"
#import "YZQPhotosCollectionViewCell.h"

@interface YZQPhotosViewController () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, retain) NSMutableArray *photosArr;
@property (nonatomic, retain) UICollectionView *imageCollectionView;
@property (nonatomic, retain) UILabel *textLabel;
@property (nonatomic, retain) NSArray *photos;
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UIImageView *backGroundImage;
@property (nonatomic, retain) UIBlurEffect *effect;
@property (nonatomic, retain) UIVisualEffectView *visEffect;
@property (nonatomic, retain) UIScrollView *scroll;
@property (nonatomic, retain) UILabel *numL;
@property (nonatomic, assign) NSInteger num;
@property (nonatomic, retain) UIImageView *myView;
@property (nonatomic, assign) CGPoint startPoint;
@property (nonatomic, assign) CGRect rect;
@property (nonatomic, assign) CGFloat dy;

@end

@implementation YZQPhotosViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createSubviews];
    
    [self getData];
}

- (void)createSubviews
{
    [self creatImageCollectionView];
    [self creatTextLabel];
    [self createNumLabel];
}

- (void)creatImageCollectionView
{

    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flow.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    flow.minimumLineSpacing = 0;
    flow.minimumInteritemSpacing = 0;
    self.imageCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) collectionViewLayout:flow];
    self.imageCollectionView.backgroundColor = [UIColor colorWithRed:0.094 green:0.090 blue:0.087 alpha:1.000];
    self.imageCollectionView.delegate = self;
    self.imageCollectionView.dataSource = self;
    self.imageCollectionView.pagingEnabled = YES;
    [self.view addSubview:self.imageCollectionView];
    [self.imageCollectionView registerClass:[YZQPhotosCollectionViewCell class] forCellWithReuseIdentifier:@"PhotoCollectionViewCell"];
    
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(50, 10, [UIScreen mainScreen].bounds.size.height, 40)];
    lable.text = self.photoTitle;
    lable.font = [UIFont boldSystemFontOfSize:20];
    lable.textColor = [UIColor whiteColor];
    [self.imageCollectionView addSubview:lable];
    
}

- (void)creatTextLabel {
    
    self.myView = [[UIImageView alloc]initWithFrame:CGRectMake(5, [UIScreen mainScreen].bounds.size.height / 1.5 + 30, [UIScreen mainScreen].bounds.size.width - 2 * 5, [UIScreen mainScreen].bounds.size.height - [UIScreen mainScreen].bounds.size.width / 1.3)];
    self.myView.alpha = 0.5;
    self.myView.userInteractionEnabled = YES;
    
    UIPanGestureRecognizer *longPress = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(longAction:)];
    [self.myView addGestureRecognizer:longPress];
    [self.view addSubview:self.myView];
    
    self.textLabel = [[UILabel alloc] init];
    self.textLabel.numberOfLines = 0;
    self.textLabel.textColor = [UIColor whiteColor];
    self.textLabel.backgroundColor = [UIColor clearColor];
    
    [self.myView addSubview:self.textLabel];
    
}

- (void) createNumLabel
{
    self.numL = [[UILabel alloc] initWithFrame:CGRectMake(20, 70, 100, 30)];
    self.numL.textColor = [UIColor whiteColor];
    [self.view addSubview:self.numL];
}

- (void)getData
{
    NSString *str = [[self.photosetID substringFromIndex:4] stringByReplacingOccurrencesOfString:@"|" withString:@"/"];
    str = [NSString stringWithFormat:@"http://c.m.163.com/photo/api/set/%@.json", str];
    
    [AFNetTool getNetWithURL:str body:nil headFile:nil responseStyle:YZQJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        self.photos = [responseObject objectForKey:@"photos"];
        [self.imageCollectionView reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
    
}

- (void)longAction:(UILongPressGestureRecognizer *)longPress
{

    if (longPress.state == UIGestureRecognizerStateBegan) {
        
        // 获取手指触摸屏幕的点
        self.startPoint =  [longPress locationInView:self.myView];
        
        
    }
    if (longPress.state == UIGestureRecognizerStateChanged) {
        
        // 获取手指触摸屏幕的点
        CGPoint point =  [longPress locationInView:self.myView];
                // 计算y轴移动的距离
        self.dy = point.y - self.startPoint.y;
        
        // 改变中心点得坐标
        self.myView.center = CGPointMake(self.myView.center.x , self.myView.center.y + self.dy);
        
        
    }
    if (longPress.state == UIGestureRecognizerStateEnded) {
        
        if (self.myView.frame.origin.y + self.dy < 667 - self.rect.size.height) {
            
            self.myView.center = CGPointMake(self.myView.frame.size.width / 2 + 8, (667 - self.rect.size.height));
            
        }
        
        if (self.myView.frame.origin.y + self.dy > 580 - 30) {
            self.myView.center = CGPointMake(self.myView.frame.size.width / 2 + 8, 640);
        }

        
    }
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.photos.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YZQPhotosCollectionViewCell *PhotoCollectionViewCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoCollectionViewCell" forIndexPath:indexPath];
    PhotoCollectionViewCell.dic = [self.photos objectAtIndex:indexPath.item];
    
    NSDictionary *dic = [self.photos objectAtIndex:indexPath.item];
    NSString *str = [dic objectForKey:@"imgurl"];
    [self.backGroundImage sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"audionews_play_bg"]];
    
    // 判断imgtitle为空就用note的内容
    if ([[[self.photos objectAtIndex:indexPath.item] objectForKey:@"imgtitle"] isEqualToString:@""]) {
        self.textLabel.text = [[self.photos objectAtIndex:indexPath.item] objectForKey:@"note"];
    } else {
        self.textLabel.text = [[self.photos objectAtIndex:indexPath.item] objectForKey:@"imgtitle"];
    }
    
    self.rect = [self.textLabel.text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 2 * 5, 0)  options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    
    self.scroll.contentSize = CGSizeMake(0, self.rect.size.height);
    self.textLabel.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 2 * 5, self.rect.size.height);
    
    self.num = indexPath.item + 1;
    self.numL.text = [NSString stringWithFormat:@"%ld / %ld", self.num, self.photos.count];
    
    return PhotoCollectionViewCell;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
